package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverClassifyAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverClassifyListAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Discover_ClassifyHeadService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Discover_Classify_Advertisement;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_ClassifyHead;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_ClassifyHeadTotle;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_Classify_ad;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_Classify_ad_Totle;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discover_ClassifyFragment extends Fragment implements Callback<Discover_ClassifyHeadTotle> {

    private List<List<Discover_ClassifyHead>> datas;
    private List<Discover_ClassifyHead> list_yuan;
    private List<Discover_ClassifyHead> list_yuan2;
    private View view;
    private ListView listView;
    private ImageView headImageView;
    private DiscoverClassifyListAdapter adapter;
    private ViewPager footViewPager;
    private LinearLayout pointContainer;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_classify, container, false);
        datas=new ArrayList<>();
        initView(view);
        initData();
        adapter=new DiscoverClassifyListAdapter(datas,getActivity());
        listView.setAdapter(adapter);
        return view;
    }

    private void initView(View view) {

        //主布局ListVIew
        listView = ((ListView) view.findViewById(R.id.list_view_gg));

        //为ListView添加头布局
        View headView = getActivity().getLayoutInflater().inflate(R.layout.classify_lv_head, null);
        headImageView = ((ImageView) headView.findViewById(R.id.lv_head));
        listView.addHeaderView(headView);

        //为ListView添加尾布局
        View footView=getActivity().getLayoutInflater().inflate(R.layout.discover_lv_tail,null);
        footViewPager = ((ViewPager) footView.findViewById(R.id.discover_lv_tail_viewpager));
        listView.addFooterView(footView);
        //白点容器
        pointContainer = ((LinearLayout) footView.findViewById(R.id.point_container));
        ImageView iv=new ImageView(getContext());





    }

    private void initData() {
        //广告viewpager以上部分数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.CLASSIFY_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Discover_ClassifyHeadService service=retrofit.create(Discover_ClassifyHeadService.class);
        service.getClassifyHead().enqueue(this);


        //广告viewpager部分数据
        Retrofit retrofitAd = new Retrofit.Builder()
                .baseUrl(Urlconfig.DISCOVER_CLASSIFY_BASE_ADVERTISEMENT)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Discover_Classify_Advertisement serviceAd=retrofitAd.create(Discover_Classify_Advertisement.class);
        serviceAd.getAd().enqueue(new Callback<Discover_Classify_ad_Totle>() {
            @Override
            public void onResponse(Call<Discover_Classify_ad_Totle> call, Response<Discover_Classify_ad_Totle> response) {
                Log.d("TAG","分类广告页面数据"+response.body().toString());
                List<Discover_Classify_ad> datas=response.body().getData();
                if (datas.size()>0) {
                    //当数据请求回来，并且内容不为空的时候就执行将请求回来的图片放入footViewPager的方法
                    setViewPagerAdapter(response);

                    //当数据请求回来，并且内容不为空的时候就执行画小白球的方法
                    drawPoint(datas);
                }

            }

            @Override
            public void onFailure(Call<Discover_Classify_ad_Totle> call, Throwable t) {

            }
        });
    }

    private void setViewPagerAdapter(Response<Discover_Classify_ad_Totle> response) {
        List<Discover_Classify_ad> datas=response.body().getData();
        List<ImageView>imageViews=new ArrayList<>();

        for (int i = 0; i < datas.size(); i++) {
            ImageView view=new ImageView(getContext());


            //让图片宽度铺满屏幕
            view.setScaleType(ImageView.ScaleType.FIT_XY);



            
            Picasso.with(getContext()).load(datas.get(i).getCover()).into(view);
            imageViews.add(view);
        }
        DiscoverClassifyAdapter adapter=new DiscoverClassifyAdapter(imageViews);
        footViewPager.setAdapter(adapter);
    }

    private void drawPoint(List<Discover_Classify_ad> datas) {
        for (int i = 0; i < datas.size(); i++) {
            View pointView=new View(getContext());
            pointView.setBackgroundResource(R.drawable.discover_classify_ad_pointselector);
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(15,15);
            pointView.setLayoutParams(params);
            pointContainer.addView(pointView);
        }
    }


    @Override
    public void onResponse(Call<Discover_ClassifyHeadTotle> call, Response<Discover_ClassifyHeadTotle> response) {

        //处理头布局ImageView数据
        inflateHead(response);

        for(int i=1;i<response.body().getList().size()-4;i+=5){
            list_yuan=new ArrayList<>();
            for(int n=i;n<i+6;n++){
              list_yuan.add(response.body().getList().get(n));
            }
            Log.d("TAG",list_yuan.size()+"------tttttttttttt-------");
            datas.add(list_yuan);
        }
        list_yuan2=new ArrayList<>();
        for(int i=25;i<29;i++){
//            list_yuan2=new ArrayList<>();

            list_yuan2.add(response.body().getList().get(i));

        }
        Log.d("TAG",list_yuan2.size()+"------tttttttttttt-------");
        datas.add(list_yuan2);
        //获取到数据以后为ListView设置适配器（避免了空指针）
        adapter=new DiscoverClassifyListAdapter(datas,getActivity());
        listView.setAdapter(adapter);
        //错误，布局初始化的太晚了
        //initView(view);
    }

    private void inflateHead(Response<Discover_ClassifyHeadTotle> response) {
        List<Discover_ClassifyHead> datas=response.body().getList();
        List<String>converPaths=new ArrayList<>();
        if(datas.size()>0){
            for (int i = 0; i < datas.size(); i++) {
                converPaths.add(datas.get(i).getCoverPath());
            }
//            headImageView
//            String icpath="http://www.baidu.com/img/logo.gif";
//            Log.d("TAG",converPaths.get(0));
            if(converPaths.get(0)!=null){
                String icpath="http://www.baidu.com/img/logo.gif";
                headImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                Picasso.with(getContext()).load(converPaths.get(0).length()!=0?converPaths.get(0):icpath).into(headImageView);
            }
//            Picasso.with(getContext()).load(converPaths.get(0).length()!=0?converPaths.get(0):icpath).into(headImageView);
        }
    }

    @Override
    public void onFailure(Call<Discover_ClassifyHeadTotle> call, Throwable t) {

    }
}
